//
//  GPProfileItem.m
//  GuaPi
//
//  Created by 23 on 2017/7/9.
//  Copyright © 2017年 Joanlove. All rights reserved.
//

#import "GPProfileItem.h"
#import "GPChatDBTool.h"



@implementation GPProfileItem


- (instancetype)initWithUserPageInfo:(GPUserPageInfoModel *)userInfo
{
    if (self = [super init]) {
        
//        self.name = userInfo.nickname;
//        self.age = userInfo.age;
////        self.phone = userInfo.phone;
//        self.sex = userInfo.sex;
//        self.avatarURL = userInfo.avatarUrl;
        
    }
    return self;
}


/**根据环信id查询记录是否存在*/
+ (BOOL)isRecordExist:(NSString *)hid
{
    //创建SQL
    NSString *sql = [NSString stringWithFormat:@"select * from %@ where hid = '%@'",DB_CHAT_TABLE_PROFILE,hid];
    
    //操作数据库
    __block BOOL isExist = NO;
    [[GPChatDBTool shareInstance] gp_selectTable:DB_CHAT_TABLE_PROFILE withSQL:sql callback:^(FMResultSet *set) {
       
        isExist = set.next;
        
    }];
    
    return isExist;
}


/**插入数据*/
- (BOOL)insertToDatabase
{
    //创建SQL
    NSString *sql = @"";
    
    //拼接SQL
    sql = [NSString stringWithFormat:@"insert or replace into %@ values(NULL,'%@','%@','%@',%zd,'%@', %zd,'%@', '%@', %zd, '%@')",DB_CHAT_TABLE_PROFILE,self.uid,self.hid,self.name,self.age,self.phone, self.sex,self.avatarURL, self.realName, self.isBlock, self.remarks];
    
    //操作数据库
    return [[GPChatDBTool shareInstance] gp_insertTable:DB_CHAT_TABLE_PROFILE  withSQL:sql];
}

/**根据uid查询*/
+ (NSArray *)selectDBWithUid:(NSString *)user_id
{
    //创建SQL
    NSString *sql = @"";
    __block NSMutableArray *arrayM = [NSMutableArray array];
    
    //拼接SQL
    sql = [NSString stringWithFormat:@"select * from %@ where uid = '%@'",DB_CHAT_TABLE_PROFILE, user_id];
    
    //操作数据库
    [[GPChatDBTool shareInstance] gp_selectTable:DB_CHAT_TABLE_PROFILE withSQL:sql callback:^(FMResultSet *set) {
        
        while (set.next) {
            GPProfileItem *item = [[self alloc] init];
            item.uid = [set stringForColumn:@"uid"];
            item.hid = [set stringForColumn:@"hid"];
            item.name = [set stringForColumn:@"name"];
            item.age = [set intForColumn:@"age"];
            item.phone = [set stringForColumn:@"phone"];
            item.sex = [set intForColumn:@"sex"];
            item.avatarURL = [set stringForColumn:@"avatarURL"];
            item.realName = [set stringForColumn:@"realName"];
            item.isBlock = [set boolForColumn:@"isBlock"];
            item.remarks = [set stringForColumn:@"remarks"];
            [arrayM addObject:item];
        }
    }];
    
    return arrayM;
}

/**根据hid查询*/
+ (NSArray *)selectDBWithHid:(NSString *)h_id
{
    //创建SQL
    NSString *sql = @"";
    __block NSMutableArray *arrayM = [NSMutableArray array];
    
    //拼接SQL
    sql = [NSString stringWithFormat:@"select * from %@ where hid = '%@'",DB_CHAT_TABLE_PROFILE, h_id];
    
    //操作数据库
    [[GPChatDBTool shareInstance] gp_selectTable:DB_CHAT_TABLE_PROFILE withSQL:sql callback:^(FMResultSet *set) {
        
        while (set.next) {
            GPProfileItem *item = [[self alloc] init];
            item.uid = [set stringForColumn:@"uid"];
            item.hid = [set stringForColumn:@"hid"];
            item.name = [set stringForColumn:@"name"];
            item.age = [set intForColumn:@"age"];
            item.phone = [set stringForColumn:@"phone"];
            item.sex = [set intForColumn:@"sex"];
            item.avatarURL = [set stringForColumn:@"avatarURL"];
            item.realName = [set stringForColumn:@"realName"];
            item.isBlock = [set boolForColumn:@"isBlock"];
            item.remarks = [set stringForColumn:@"remarks"];
            [arrayM addObject:item];
        }
    }];
    
    return arrayM;
}

/**根据uid更新内容*/
- (BOOL)updateToDatabase
{
    //创建SQL
    NSString *sql = @"";
    
    //拼接SQL
    sql = [NSString stringWithFormat:@"update %@ set name = '%@', age = %zd, phone = '%@', sex = %zd, avatarURL = '%@', realName = '%@', isBlock = %zd where uid = '%@'",DB_CHAT_TABLE_PROFILE,self.name, self.age, self.phone, self.sex, self.avatarURL, self.realName, self.isBlock, self.uid];
    
    //操作数据库
    return [[GPChatDBTool shareInstance] gp_updateTable:DB_CHAT_TABLE_PROFILE withSQL:sql];
}


@end
